﻿using Acme.BookStore.AcmeModel;
using Acme.BookStore.Creditor;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    /// <summary>
    /// 贷主合同
    /// </summary>
    public class CreditorServer :
        CrudAppService<
            CreditorInfo, //The Book entity
            CreditorInfoDto, //Used to show books
            Guid, //Primary key of the book entity
            PagedAndSortedResultRequestDto, //Used for paging/sorting
            CreditorInfos>, //Used to create/update a book
        ICreditorInfo //implement the IBookAppService
    {
        public IRepository<CreditorInfo, Guid> _Creditor;
        public IRepository<UploadInfo, Guid> _Upload;
        public IRepository<ManagementInfo, Guid> _Management;
        public IRepository<PuserInfo, Guid> _Puser;
        public IRepository<EnumInfo, Guid> _Enum;
        public IRepository<CompanyInfo, Guid> _Company;
        public CreditorServer(IRepository<CreditorInfo, Guid> Creditor, IRepository<UploadInfo, Guid> Upload, IRepository<ManagementInfo, Guid> Management, IRepository<PuserInfo, Guid> Puser, IRepository<EnumInfo, Guid> Enum, IRepository<CompanyInfo, Guid> Company)
            : base(Creditor)
        {
            _Creditor = Creditor;
            _Upload = Upload;
            _Management = Management;
            _Puser = Puser;
            _Enum = Enum;
            _Company = Company;
        }
        //显示
        public List<CreditorInfoDto> GetShow()
        {
            var str = (from a in _Creditor
                       join b in _Management
                       on a.ManagementYid equals b.Id.ToString()
                       join c in _Puser
                       on a.UserYid equals c.Id.ToString()
                       join d in _Enum
                       on a.EnumSid equals d.Id.ToString()
                       join e in _Upload
                       on a.FileYId equals e.Id.ToString()
                       join f in _Company
                       on a.CompanyYid equals f.Id.ToString()
                       select new CreditorInfoDto
                       {
                           Id=a.Id,
                           CreditoNumber = a.CreditoNumber,
                           CreditoTitle = a.CreditoTitle,
                           CreditoRow = a.CreditoRow,
                           CreditoPrix = a.CreditoPrix,
                           CreditoBiz = a.CreditoBiz,
                           CreditoBmoney = a.CreditoBmoney,
                           CreditoMoney = a.CreditoMoney,
                           CreditoNow = a.CreditoNow,
                           CreditoTerm = a.CreditoTerm,
                           CreditoQtime = a.CreditoQtime,
                           CreditoTime = a.CreditoTime,
                           CreditoRove = a.CreditoRove,
                           CreditoRoveRemark = a.CreditoRoveRemark,
                           FileYId = a.FileYId,
                           CompanyYid = a.CompanyYid,
                           ManagementYid = a.ManagementYid,
                           UserYid = a.UserYid,
                           EnumSid = a.EnumSid,
                           FileName = e.FileName,
                           CompanyName = f.CompanyName,
                           UserName = c.UserName,
                           ManagementName = b.ManagementName,
                       }).ToList();
            return str;
        }
        [HttpPost]
        //添加
        public async Task<int> GetAdd(CreditorInfos m)
        {
            CreditorInfo str = ObjectMapper.Map<CreditorInfos, CreditorInfo>(m);
            await _Creditor.InsertAsync(str);
            return 1;
        }
        [HttpDelete]
        //删除
        public async Task<int> GetDel(Guid Id)
        {
            await _Creditor.DeleteAsync(Id);
            return 1;
        }
        //反填
        public async Task<CreditorInfoDto> GetFt(Guid Id)
        {
            CreditorInfo str = await _Creditor.GetAsync(Id);
            return ObjectMapper.Map<CreditorInfo, CreditorInfoDto>(str);
        }
        //修改
        public async Task<int> GetUpd(CreditorInfoDto m)
        {
            CreditorInfo Str = await _Creditor.GetAsync(m.Id);
            ManagementInfo Atr = await _Management.GetAsync(m.Id);
            EnumInfo Btr = await _Enum.GetAsync(m.Id);
            UploadInfo Ptr = await _Upload.GetAsync(m.Id);
            PuserInfo Ftr = await _Puser.GetAsync(m.Id);
            CompanyInfo Ctr = await _Company.GetAsync(m.Id);
            Str.CreditoNumber = m.CreditoNumber;
            Str.CreditoTitle = m.CreditoTitle;
            Str.CreditoRow = m.CreditoRow;
            Str.CreditoPrix = m.CreditoPrix;
            Str.CreditoBiz = m.CreditoBiz;
            Str.CreditoBmoney = m.CreditoBmoney;
            Str.CreditoQtime = m.CreditoQtime;
            Str.CreditoTime = m.CreditoTime;
            Str.CreditoRove = m.CreditoRove;
            Atr.ManagementName = m.ManagementName;
            Ctr.CompanyName = m.CompanyName;
            Ftr.UserName = m.UserName;
            Btr.OwnerType = m.OwnerType;
            Ptr.FileName = m.FileName;
            return 1;
        }
        //修改状态
        public async Task<int> GetxUpd(CreditorInfoDto m)
        {
            EnumInfo Btr = await _Enum.GetAsync(m.Id);
            Btr.OwnerType = 5;
            return 1;
        }
    }
}
